package org.xiaov.lunasql.spring.proxy;

import lombok.Getter;
import lombok.Setter;
import org.xiaov.lunasql.engine.config.LunaSqlConfig;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * <p>
 * DataSourceProxy
 * </p>
 *
 * @author xiaovcloud
 * @since 2024/1/23 10:50
 */
@Setter
@Getter
public class LunaDataSourceProxy extends AbstractDataSourceProxy implements DataSource {
    //    private String dbType;
//    private String url;
    private LunaSqlConfig lunaConfig;

    /**
     * 构造函数
     *
     * @param targetDataSource targetDataSource
     */
    public LunaDataSourceProxy(DataSource targetDataSource) {
        this.targetDataSource = targetDataSource;
//        init();
    }

//    private void init() {
//        try {
//            this.url = targetDataSource.getConnection().getMetaData().getURL();
//            this.dbType = LunaJdbcUtils.getDbType(url);
//        } catch (SQLException e) {
//            throw new IllegalStateException("can not init dataSource", e);
//        }
//    }

    @Override
    public Connection getConnection() throws SQLException {
        Connection connection = targetDataSource.getConnection();
        return new LunaConnectionProxy(connection, lunaConfig);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        Connection connection = targetDataSource.getConnection(username, password);
        return new LunaConnectionProxy(connection, lunaConfig);
    }
}
